package com.example.opengldemo.vertex_shader_more.file_version_1;

import android.opengl.GLES30;

import com.example.opengldemo.util.MatrixState;

import static com.example.opengldemo.vertex_shader_more.file_version_1.ParticleDataConstant.*;

 class ParticleSystem implements Comparable<ParticleSystem>
{
	public float[] startColor;
	public float[] endColor;
	public int srcBlend;
	public int dstBlend;
	public int blendFunc;
	public float maxLifeSpan;
	public float lifeSpanStep;
	public int sleepSpan;
	public int groupCount;
	public float sx;
	public float sy;
	float positionX;
	float positionZ;
	public float xRange;
	public float yRange;
	public float vx;
	public float vy;
	float yAngle=0;
	ParticleForDraw fpfd;
	boolean flag=true;
	float halfSize;
	
	public float[] points;
	
    public ParticleSystem(float positionx,float positionz,ParticleForDraw fpfd,int count)
    {
    	//火堆位置
    	this.positionX=positionx;
    	this.positionZ=positionz;
    	// 开始颜色
    	this.startColor=START_COLOR[CURR_INDEX];
    	// 结束颜色
    	this.endColor=END_COLOR[CURR_INDEX];
    	// 混合方式
    	this.srcBlend=SRC_BLEND[CURR_INDEX];
    	// 混合方式
    	this.dstBlend=DST_BLEND[CURR_INDEX];
    	// 混合方式 方法
    	this.blendFunc=BLEND_FUNC[CURR_INDEX];
    	// 最大的标准
    	this.maxLifeSpan=MAX_LIFE_SPAN[CURR_INDEX];
    	//生命周期步长
    	this.lifeSpanStep=LIFE_SPAN_STEP[CURR_INDEX];
    	// 组数
    	this.groupCount=GROUP_COUNT[CURR_INDEX];
    	// 睡眠延时
    	this.sleepSpan=THREAD_SLEEP[CURR_INDEX];

    	this.sx=0;
    	this.sy=0;

    	this.xRange=X_RANGE[CURR_INDEX];
    	this.yRange=Y_RANGE[CURR_INDEX];

    	this.vx=0;
    	this.vy=VY[CURR_INDEX];

    	this.halfSize=RADIS[CURR_INDEX];
    	
    	this.fpfd=fpfd;
    	
    	this.points=initPoints(count);
    	fpfd.initVertexData(points);
    	new Thread()
    	{//�������ӵĸ����߳�
    		public void run()//��дrun����
    		{
    			while(flag)
    			{
    				update();
    				try 
    				{
						Thread.sleep(sleepSpan);
					} catch (InterruptedException e) 
					{
						e.printStackTrace();
					}
    			}
    		}
    	}.start();
    }
    
	public float[] initPoints(int zcount)
	{	// 点的总数
		float[] points=new float[zcount*4*6];
		for(int i=0;i<zcount;i++)
		{

			float px=(float) (sx+xRange*(Math.random()*2-1.0f));
	        float py=(float) (sy+yRange*(Math.random()*2-1.0f));
	        float vx=(sx-px)/150;
	        
	        points[i*4*6]=px-halfSize/2;
	        points[i*4*6+1]=py+halfSize/2;
	        points[i*4*6+2]=vx;
	        points[i*4*6+3]=10.0f;
			
	        points[i*4*6+4]=px-halfSize/2;
	        points[i*4*6+5]=py-halfSize/2;
	        points[i*4*6+6]=vx;
	        points[i*4*6+7]=10.0f;
			
	        points[i*4*6+8]=px+halfSize/2;
	        points[i*4*6+9]=py+halfSize/2;
	        points[i*4*6+10]=vx;
	        points[i*4*6+11]=10.0f;
			
	        points[i*4*6+12]=px+halfSize/2;
	        points[i*4*6+13]=py+halfSize/2;
	        points[i*4*6+14]=vx;
	        points[i*4*6+15]=10.0f;
			
	        points[i*4*6+16]=px-halfSize/2;
	        points[i*4*6+17]=py-halfSize/2;
	        points[i*4*6+18]=vx;
	        points[i*4*6+19]=10.0f;
			
	        points[i*4*6+20]=px+halfSize/2;
	        points[i*4*6+21]=py-halfSize/2;
	        points[i*4*6+22]=vx;
	        points[i*4*6+23]=10.0f;
		}
		for(int j=0;j<groupCount;j++)
	    {
			points[4*j*6+3]=lifeSpanStep;
			points[4*j*6+7]=lifeSpanStep;
			points[4*j*6+11]=lifeSpanStep;
			points[4*j*6+15]=lifeSpanStep;
			points[4*j*6+19]=lifeSpanStep;
			points[4*j*6+23]=lifeSpanStep;
			
	    }
		return points;
	}
    

	
    public void drawSelf(int texId)
    {
    	
        GLES30.glDisable(GLES30.GL_DEPTH_TEST);
        GLES30.glEnable(GLES30.GL_BLEND);
        GLES30.glBlendEquation(blendFunc);
        GLES30.glBlendFunc(srcBlend,dstBlend);
        
    	MatrixState.translate(positionX, 1, positionZ);
		MatrixState.rotate(yAngle, 0, 1, 0);
		
		MatrixState.pushMatrix();
    	fpfd.drawSelf(texId,startColor,endColor,maxLifeSpan);
		MatrixState.popMatrix();
    	
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
        GLES30.glDisable(GLES30.GL_BLEND);
    }
    
	int count=1;
	public void update()
	{	
		if(count>=(points.length/groupCount/4/6))
		{
			count=0;
		}
		

		for(int i=0;i<points.length/4/6;i++)
		{
			if(points[i*4*6+3]!=10.0f)
			{
				points[i*4*6+3]+=lifeSpanStep;
				points[i*4*6+7]+=lifeSpanStep;
				points[i*4*6+11]+=lifeSpanStep;
				points[i*4*6+15]+=lifeSpanStep;
				points[i*4*6+19]+=lifeSpanStep;
				points[i*4*6+23]+=lifeSpanStep;
	    		if(points[i*4*6+3]>this.maxLifeSpan)
	    		{
	    			float px=(float) (sx+xRange*(Math.random()*2-1.0f));
	                float py=(float) (sy+yRange*(Math.random()*2-1.0f));
	                float vx=(sx-px)/150;
	                
	                points[i*4*6]=px-halfSize/2;
	    	        points[i*4*6+1]=py+halfSize/2;
	    	        points[i*4*6+2]=vx;
	    	        points[i*4*6+3]=10.0f;
	    			
	    	        points[i*4*6+4]=px-halfSize/2;
	    	        points[i*4*6+5]=py-halfSize/2;
	    	        points[i*4*6+6]=vx;
	    	        points[i*4*6+7]=10.0f;
	    			
	    	        points[i*4*6+8]=px+halfSize/2;
	    	        points[i*4*6+9]=py+halfSize/2;
	    	        points[i*4*6+10]=vx;
	    	        points[i*4*6+11]=10.0f;
	    			
	    	        points[i*4*6+12]=px+halfSize/2;
	    	        points[i*4*6+13]=py+halfSize/2;
	    	        points[i*4*6+14]=vx;
	    	        points[i*4*6+15]=10.0f;
	    			
	    	        points[i*4*6+16]=px-halfSize/2;
	    	        points[i*4*6+17]=py-halfSize/2;
	    	        points[i*4*6+18]=vx;
	    	        points[i*4*6+19]=10.0f;
	    			
	    	        points[i*4*6+20]=px+halfSize/2;
	    	        points[i*4*6+21]=py-halfSize/2;
	    	        points[i*4*6+22]=vx;
	    	        points[i*4*6+23]=10.0f;
	    		}else
	    		{
	    			 points[i*4*6]+=points[i*4*6+2];
	                 points[i*4*6+1]+=vy;
	                 
	                 points[i*4*6+4]+=points[i*4*6+6];
	                 points[i*4*6+5]+=vy;
	                 
	                 points[i*4*6+8]+=points[i*4*6+10];
	                 points[i*4*6+9]+=vy;
	                 
	                 points[i*4*6+12]+=points[i*4*6+14];
	                 points[i*4*6+13]+=vy;
	                 
	                 points[i*4*6+16]+=points[i*4*6+18];
	                 points[i*4*6+17]+=vy;
	                 
	                 points[i*4*6+20]+=points[i*4*6+22];
	                 points[i*4*6+21]+=vy;
	    		}
			}
		}
		
		for(int i=0;i<groupCount;i++)
		{
			if(points[groupCount*count*4*6+4*i*6+3]==10.0f)
			{
				points[groupCount*count*4*6+4*i*6+3]=lifeSpanStep;
				points[groupCount*count*4*6+4*i*6+7]=lifeSpanStep;
				points[groupCount*count*4*6+4*i*6+11]=lifeSpanStep;
				points[groupCount*count*4*6+4*i*6+15]=lifeSpanStep;
				points[groupCount*count*4*6+4*i*6+19]=lifeSpanStep;
				points[groupCount*count*4*6+4*i*6+23]=lifeSpanStep;
			}
		}
		synchronized(lock)
		{
			fpfd.updatVertexData(points);
		}
		count++;
	}
    public static float cx = 0 ;
    public static float cz = 0 ;
	public void calculateBillboardDirection()
	{
		float xspan=positionX-cx;
		float zspan=positionZ-cz;
		
		if(zspan<=0)
		{
			yAngle=(float)Math.toDegrees(Math.atan(xspan/zspan));	
		}
		else
		{
			yAngle=180+(float)Math.toDegrees(Math.atan(xspan/zspan));
		}
	}
	@Override
	public int compareTo(ParticleSystem another) {

		float xs=positionX-cx;
		float zs=positionZ-cz;
		
		float xo=another.positionX-cx;
		float zo=another.positionZ-cz;
		
		float disA=(float)(xs*xs+zs*zs);
		float disB=(float)(xo*xo+zo*zo);
		return ((disA-disB)==0)?0:((disA-disB)>0)?-1:1;  
	}

}
